package cn.iocoder.yudao.module.system.service.monitor;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.monitor.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.monitor.MonitorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.monitor.MonitorMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 监控内容 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MonitorServiceImpl implements MonitorService {

    @Resource
    private MonitorMapper monitorMapper;

    @Override
    public Long createMonitor(MonitorSaveReqVO createReqVO) {
        // 插入
        MonitorDO monitor = BeanUtils.toBean(createReqVO, MonitorDO.class);
        monitorMapper.insert(monitor);
        // 返回
        return monitor.getId();
    }

    @Override
    public void updateMonitor(MonitorSaveReqVO updateReqVO) {
        // 校验存在
        validateMonitorExists(updateReqVO.getId());
        // 更新
        MonitorDO updateObj = BeanUtils.toBean(updateReqVO, MonitorDO.class);
        monitorMapper.updateById(updateObj);
    }

    @Override
    public void deleteMonitor(Long id) {
        // 校验存在
        validateMonitorExists(id);
        // 删除
        monitorMapper.deleteById(id);
    }

    @Override
        public void deleteMonitorListByIds(List<Long> ids) {
        // 校验存在
        validateMonitorExists(ids);
        // 删除
        monitorMapper.deleteByIds(ids);
        }

    private void validateMonitorExists(List<Long> ids) {
        List<MonitorDO> list = monitorMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MONITOR_NOT_EXISTS);
        }
    }

    private void validateMonitorExists(Long id) {
        if (monitorMapper.selectById(id) == null) {
            throw exception(MONITOR_NOT_EXISTS);
        }
    }

    @Override
    public MonitorDO getMonitor(Long id) {
        return monitorMapper.selectById(id);
    }

    @Override
    public PageResult<MonitorDO> getMonitorPage(MonitorPageReqVO pageReqVO) {
        return monitorMapper.selectPage(pageReqVO);
    }

}